5 import java
.util
.ArrayList
;
6 import java
.util
.Observable
;
7 import java
.util
.Observer
;
8 import java
.util
.Scanner
;
11 public class GraphModel
extends Observable
implements Observer
{
12 private List
<GraphEdge
> edges
;
13 private List
<GraphVertex
> vertexes
;
16 public GraphVertex
[] getVertexes(){
17 GraphVertex
[] a
= new GraphVertex
[vertexes
.size()];
18 return vertexes
.toArray(a
);
20 public GraphEdge
[] getEdges(){
21 GraphEdge
[] a
= new GraphEdge
[edges
.size()];
22 return edges
.toArray(a
);
26 edges
= new ArrayList
<GraphEdge
>();
27 vertexes
= new ArrayList
<GraphVertex
>();
30 public synchronized void setChanged() {
35 public void addEdge(GraphEdge
... edge
){
36 for(int i
=0; i
< edge
.length
; i
++){
38 edge
[i
].addObserver(this);
43 public void addGraph(GraphVertex
... vertex
){
44 for(int i
=0; i
< vertex
.length
; i
++){
45 vertexes
.add(vertex
[i
]);
46 vertex
[i
].addObserver(this);
51 public void removeEdge(GraphEdge
... edge
){
52 for(int i
=0; i
< edge
.length
; i
++){
53 edges
.remove(edge
[i
]);
54 edge
[i
].deleteObserver(this);
58 public void removeGraph(GraphVertex
... vertex
){
59 for(int i
=0; i
< vertex
.length
; i
++){
60 vertexes
.remove(vertex
[i
]);
61 vertex
[i
].deleteObserver(this);
66 public GraphVertex
getGraphByCoordinate(int x
, int y
){
67 for(int i
=0; i
< vertexes
.size(); i
++){
68 if(vertexes
.get(i
).isAt(x
, y
))
69 return vertexes
.get(i
);
74 public void saveModel() throws IOException
{
75 PrintWriter pw
= new PrintWriter(new FileWriter("file.graph"));
76 pw
.println(vertexes
.size() + "," + edges
.size());
81 public void update(Observable arg0
, Object arg1
) {
82 if(vertexes
.contains(arg0
) || edges
.contains(arg0
)){
89 public void writeToFile(File file
) throws Exception
91 FileOutputStream fos
= new FileOutputStream(file
);
92 OutputStreamWriter out
= new OutputStreamWriter(fos
, "UTF-8");
94 out
.write(vertexes
.size() + " " + edges
.size() + "\r\n");
95 for(int i
=0; i
< vertexes
.size(); i
++)
96 out
.write(vertexes
.get(i
).getX() + " " + vertexes
.get(i
).getY() + " " + vertexes
.get(i
).getWidth() + " " + vertexes
.get(i
).getHeight() +" "+ vertexes
.get(i
).getName() + "\r\n");
97 for(int i
=0; i
< edges
.size(); i
++)
98 out
.write(vertexes
.indexOf(edges
.get(i
).getEdge(false)) + " " + vertexes
.indexOf(edges
.get(i
).getEdge(true)) + "\r\n");
107 private static GraphVertex
[] readVertexes(int count
, Scanner s
)
109 GraphVertex
[] vertexes
= new GraphVertex
[count
];
110 for(int i
=0; i
< count
; i
++)
111 vertexes
[i
] = new GraphVertex(s
);
116 public static GraphModel
fromFile(File file
) throws Exception
118 GraphModel model
= new GraphModel();
119 Scanner scanner
= new Scanner(file
, "UTF-8");
121 int numKnopen
= scanner
.nextInt();
122 int numKanten
= scanner
.nextInt();
125 GraphVertex
[] vertexes
= readVertexes(numKnopen
,scanner
);
127 model
.addEdge(readKanten(scanner
, numKanten
, vertexes
));
128 model
.addGraph(vertexes
);
132 private static GraphEdge
[] readKanten(Scanner scanner
, int numKanten
,
133 GraphVertex
[] vertexes
) {
134 GraphEdge
[] edges
= new GraphEdge
[numKanten
];
135 for (int i
= 0; i
< numKanten
; i
++) {
136 int obj1
= scanner
.nextInt();
137 int obj2
= scanner
.nextInt();
138 edges
[i
] = new GraphEdge(vertexes
[obj1
], vertexes
[obj2
]);
139 if(i
!= (numKanten
-1)){
147 public static GraphModel
fromFile(String fileName
) throws Exception
149 File file
= new File(fileName
);
150 return fromFile(file
);